local scene = gamestate.new()

local x = 100
local y = 250
local w = 300
local h = 50
local off = 20

function scene:enter(from,game)
	self.game = game
	local sw,sh = love.graphics.getDimensions()
	x = sw*GlobalScale/2 - w/2
	self.ui = {
		id = {text = "",mono = true},
		password = {text = "",password = true,mono = true},
		submit = {text = "提交"}
	}
end

function scene:update(dt)
	love.graphics.setFont(texture.font)
	suit.Label("用户登录",x,y-100,w,h)
	local offy = 0
	
    local b = self.ui.id
   	suit.Label("用户",x-140,y+offy,100,h)
    local r = suit.Input(b,b,x,y+offy,w,h)
    if r.hit then
    	love.keyboard.setTextInput( true, x/GlobalScale,(y+offy)/GlobalScale,w/GlobalScale,h/GlobalScale)
    elseif r.submitted then
    	love.keyboard.setTextInput( false)
    end
	offy = offy + (h + off)

	local b = self.ui.password
	suit.Label("密码",x-140,y+offy,100,h)
    local r = suit.Input(b,b,x,y+offy,w,h)
    if r.hit then
    	love.keyboard.setTextInput( true, x/GlobalScale,(y+offy)/GlobalScale,w/GlobalScale,h/GlobalScale)
    elseif r.submitted then
    	love.keyboard.setTextInput( false)
    end
    offy = offy + (h + off)

    local b = self.ui.submit
    local r = suit.Button(b.text,b,x,y+offy,w,h)
    if r.entered then
        sound:play("hover")
    end
    if r.hit then
        net.client:send("login",{id = self.ui.id.text,password = self.ui.password.text})
    end
    self.game:update(dt)
end

function scene:userLogin(ok,data)
	if ok then
		user:setData(data)
		gamestate.switch(gameState.title,self.game)
	else
		self.ui.password.pretext = ""
		self.ui.id.pretext = "用户名或密码错误"
	end
end

function scene:draw()
	self.game:draw()
	suit.draw()
end

function scene:keypressed(k)
	if k == "escape" then
		gamestate.switch(gameState.title,self.game)
	end
end

return scene